<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$srcDbHdr    = "MUT_OPN_HDR";
$srcDbDtl    = "MUT_OPN_DTL";
$srcDbHdrLog = "LOG_MUT_OPN_HDR";

$UID     = unserialize($_SESSION["serUID"]);
$userId  = $UID->getUID();

$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
           $_SESSION["OPASS"],
           $_SESSION["SID"],
           _NO_COMMENT);

if($Db->connect()){

	/**
	* Delete the previous entry
	*
	*/
	$CHandlers->dbEntDelete($Db,
            	            $_SESSION["jid"],
            	            $srcDbHdr);

  $CHandlers->dbEntDelete($Db,
            	            $_SESSION["jid"],
            	            $srcDbDtl);

  $CHandlers->dbEntDelete($Db,
                          $_SESSION["jid"],
                          $SourceDb->fetchValue("srcDbInputHistory"));

  /**
  * We don't build up any jobId anymore since this is
  * a modify mode. Also we already have the previous
  * jobId in $_SESSION
  *
  */
  $jobId = $_SESSION["jid"];

  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."' , 'dd-mm-YYYY')";

  $CFetchers->fetchModelId($Db,
                           $_POST["modelEnt"],
                           $SourceDb->fetchValue("srcDbModel"),
                           $modelId);

  $CFetchers->fetchIsSerNumber($Db,
                               $_POST["modelEnt"],
                               $modelId,
                               $SourceDb->fetchValue("srcDbModel"),
                               $isSerNumber);

  if($isSerNumber == 1){

    /**
     * since this is a SN
     * insert into SN detail table
     */
    $values = " '".$jobId."',   ".
              "  1 ,            ".
              " '".$modelId."', ".
              "  ".$_POST["headEnt"][$iRow]." ,   ".
              "  ".$_POST["lengthEnt"][$iRow]." , ".
              "  1 ,                              ".
              " '".$_POST["serNumEnt"][$iRow]."'  ";
  }else{

    /* Do nothing */
  }

  if($_POST["qtyEnt"] >= 0){

    $txType = _OPNAME_PLUS;
  }else{

    $txType = _OPNAME_MINUS;
  }

  for($iRow = 0; $iRow < $dataup ; $iRow++){

    if($isSerNumber == 1){

      $values = " '".$jobId."',    ".
                "  ".($iRow+1)." , ".
                "  ".$_POST["priceEnt"][$iRow]." ,  ".
                "  ".$_POST["headEnt"][$iRow]." ,   ".
                "  ".$_POST["lengthEnt"][$iRow]." , ".
                " '".$_POST["serNumEnt"][$iRow]."'  ";
    }else{

      $values = " '".$jobId."',    ".
                "  ".($iRow+1)." , ".
                "  ".$_POST["priceEnt"][$iRow]." ,  ".
                "  0 , ".
                "  0 , ".
                " ''   ";
    }

    $CLHandlers->insToDtl($Db,
                          $values,
                          $SourceDb->fetchValue("srcDbDtl"));

    $valTx   =  " '".$jobId."', ".                       /* Job id     */
                "  ".$hdrDate." , ".                     /* Tr Date    */
                " '".$modelId."', ".                     /* Model Id   */
                "  ".$_POST["qtyEnt"]." , ".             /* quantity   */
                "  ".$_POST["priceEnt"][$iRow]." , ".    /* Model price */
                "  ".$_POST["priceEnt"][$iRow].",  ".    /* net total */
                "  ".($iRow + 1).", ".                   /* Source detail number */
                "  ".$txType."  ";                       /* Tx Type        */

    $CHandlers->insTxHistory($Db,
                             $valTx,
                             $SourceDb->fetchValue("srcDbInputHistory"));
  }

  $CFetchers->fetchHdrNo($Db,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);

  $values = " '".$jobId."',    ".
            " ' ',             ".
            "  ".$hdrDate." ,  ".
            "  ".$hdrNo." ,    ".
            " '".$_POST["whsEnt"]."',    ".
            " '".$modelId."',            ".
            "  ".$_POST["qtyEnt"]." ,    ".
            " '".$_POST["memoEnt"]."',   ".
            " '"._NOT_ACTIVE."'          ";


  $CLHandlers->insToHdr($Db,
                        $values,
                        $SourceDb->fetchValue("srcDbHdr"));

  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($Db,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);

	$Db->commit();

	if($Db->getStatus() == true){

	  $Misc->usrMsg("Modify Complete");
	}else{

	  $Misc->usrDieJs("Modify Process Interupted");
	}

	$Db->dc();

	/**
	* Pending...
	* I'm not sure if this
	* reset procedure off
	* causes buggy
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/
	//jumpTo(2,"../".$_SESSION["source_caller"]);

}else{

	$Misc->usrDieJs("Db connection failed");
}

?>